<template>
    <div class="login-container">
        <div class="login-box">
            <h1 class="login-title">用户登录</h1>
            <a-form
                ref="formRef"
                :model="formState"
                name="basic"
                :label-col="{ span: 8 }"
                :wrapper-col="{ span: 16 }"
                autocomplete="off"
                @finish="onFinish"
                @finishFailed="onFinishFailed"
            >
                <a-form-item
                    :label="setcolor('用户名')"
                    name="username"
                    :rules="[{ required: true, message: '请输入用户名！' }]"
                >
                    <a-input
                        v-model="formState.username"
                        placeholder="请输入用户名"
                        aria-label="用户名"
                    />
                </a-form-item>

                <a-form-item
                    :label="setcolor('密码')"
                    name="password"
                    :rules="[{ required: true, message: '请输入密码！' }]"
                >
                    <a-input-password
                        v-model="formState.password"
                        placeholder="请输入密码"
                        aria-label="密码"
                    />
                </a-form-item>

                <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
                    <div class="form-actions">
                        <a-button
                            type="primary"
                            :loading="isLoading"
                            html-type="submit"
                            @click="toHome"
                        >
                            登录
                        </a-button>
                        <a href="/register" class="register-link"> 没有账号?去注册 </a>
                    </div>
                </a-form-item>
            </a-form>
        </div>
    </div>
</template>

<script>
import Vue from 'vue';

import { message } from 'ant-design-vue';

export default {
    data() {
        return {
            isLoading: false,
            formState: {
                username: '',
                password: '',
            },
        };
    },
    methods: {
        setcolor(name) {
            return Vue.h('span', { style: { color: 'white' } }, name);
        },
        onFinish(values) {
            this.formState.username = values.username;
            this.formState.password = values.password;
        },
        onFinishFailed() {
            message.error('表单验证失败');
        },
        async toHome() {
            try {
                this.isLoading = true;

                const params = {
                    username: this.formState.username,
                    password: this.formState.password,
                };

                const res = await this.$service.post('/login', params);

                if (res) {
                    message.success('登录成功！');
                    this.$store.commit('app/setToken', res.token);
                    this.$store.commit('counter/setCredentials', {
                        username: this.formState.username,
                        password: this.formState.password,
                    });
                    this.$router.push({ path: '/home' });
                } else {
                    message.error('账号或密码错误！');
                }
            } catch (error) {
                message.error('登录失败，请稍后重试');
            } finally {
                this.isLoading = false;
            }
        },
    },
};
</script>

<style lang="scss" scoped>
.login-container {
    width: 100%;
    height: 100vh;
    position: fixed;
    background: url('~@/assets/bg1.png') center/cover no-repeat;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-box {
    width: 600px;
    padding: 40px;
    background: rgba(0, 0, 86, 0.6);
    border-radius: 5px;
    border: 2px solid rgba(0, 177, 255, 0.9);
}

.login-title {
    font-size: 30px;
    color: #fff;
    text-align: center;
    margin-bottom: 40px;
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}

.form-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 24px;
}

.register-link {
    color: #fff;
    text-decoration: underline;

    &:hover {
        color: #40a9ff;
    }
}
</style>




